package keter.util;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by gulx@neusoft.com on 2018/3/7.
 */
public class ExecutorUtil {

    public static ExecutorService getExecutorService(String name, int poolSize) {
        return getExecutorService(name,poolSize,true);
    }

    public static ExecutorService getExecutorService(String name, int poolSize, boolean daemon){

        BasicThreadFactory factory = new BasicThreadFactory.Builder()
                .namingPattern(name)
                .daemon(daemon) // the JVM shuts down if there are only daemon threads running.
                .build();

        return new ThreadPoolExecutor(
                poolSize,
                poolSize,
                0L,TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                factory,
                new ThreadPoolExecutor.AbortPolicy()
        );
    }
}
